home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / allocwg.com / TEST.C < prev   
Encoding:
C/C++ Source or Header  |  1988-11-10  |  6.2 KB  |  166 lines

  1. #include <stdio.h>
  2.  
  3.    char *buffer [300] ;
  4.  
  5. main ()
  6.  
  7. /*
  8.    +-----------------------------------------------+
  9.    |                                               |
  10.    |  This is the test program for comparing       |
  11.    |  the new memory allocation procedures and     |
  12.    |  those of MSC.  To compile for MSC, do:       |
  13.    |                                               |
  14.    |     cl /c /AL /DMSC *.c                       |
  15.    |     linker                                    |
  16.    |                                               |
  17.    |  To compile for the new allocation procs, do: |
  18.    |                                               |
  19.    |     cl /c /AL *.c                             |
  20.    |     linker                                    |
  21.    |                                               |
  22.    |  For either set, clear the screen and then    |
  23.    |  run the test program.  The MSC test will     |
  24.    |  perform a series of allocation, reallocation,|
  25.    |  free types of activities.  After each        |
  26.    |  activity, a dump of the heap is appended to  |
  27.    |  a file named 'memory.msc'.  The alternate    |
  28.    |  test will perform the same activities        |
  29.    |  except it will write the heap information    |
  30.    |  to a file called 'memory.new'.  You may      |
  31.    |  compare the two files for performance.       |
  32.    |                                               |
  33.    |  While the test is running, the current size  |
  34.    |  of various things is displayed on the top    |
  35.    |  line of the screen (see 'dispsize.c' for     |
  36.    |  expanation).                                 |
  37.    |                                               |
  38.    +-----------------------------------------------+
  39. */
  40.  
  41. {
  42.    int   i, j, max   ;
  43.    void *malloc  ()  ;
  44.    void *realloc ()  ;
  45.    void  free    ()  ;
  46.    char *bufptr      ;
  47.    int   mem         ;
  48.  
  49. #ifndef MSC
  50.    ReduceDefaultData () ;      /*  Reduce default data size           */
  51.    printf ( "\n** Non-MSC memory test **\n" ) ;
  52. #else
  53.    extern size_t _amblksiz ;
  54.    _amblksiz = 1024 ;          /*  Set allocation block size the same
  55.                                    for both tests.                    */
  56.    printf ( "\n** MSC memory test **\n" ) ;
  57.  
  58.    /*  NOTE: a similar effect to ReduceDefaultData can be achieved 
  59.              for MSC by running 'exemod' on this test program to 
  60.              set the max paragraph size to the min paragraph size.    */
  61. #endif
  62.  
  63.    max = 0 ;
  64.    srand ( 12345) ;                      /*  Init. random number generator */
  65.                                          /*  This can be changed to 
  66.                                              different values to get 
  67.                                              varying results.              */
  68.  
  69.       /*  First do some straight memory allocations  */
  70.  
  71.    printf ( "Starting Allocation\n" ) ;
  72.    for ( i = 0 ; i < 300 ; i++ ) {
  73.       mem  = randnum ( 1, 1024  ) ;      /*  Generate allocation size      */
  74.                                          /*  between 1 and 1024            */
  75.       buffer [i] = (char *) malloc ( mem ) ;
  76.       if ( buffer [i] == NULL ) {
  77.          printf ( "Unable to allocate memory buffer %d\n", i ) ;
  78.          break ;
  79.       } ;
  80.       DisplaySize () ;                   /*  Display program size          */
  81.       max++ ;
  82.    } ;
  83.    DumpHeap ( "After all Allocations" ) ; 
  84.  
  85.       /*  Next, reallocate blocks to different sizes starting with
  86.           the last block allocated and working down to the lowest   */
  87.  
  88.    printf ( "Starting Reallocation - Top down\n" ) ;
  89.    for ( j = max-1 ; j <= 0 ; j++ ) {
  90.       mem  = randnum ( 1, 1024  ) ;      /*  Generate reallocation size    */
  91.                                          /*  between 1 and 1024            */
  92.       if ( buffer [j] != NULL ) {
  93.          bufptr = (char *) realloc ( buffer [j], mem ) ;
  94.          if ( bufptr == NULL )
  95.             printf ( "Unable to realloc buffer %d\n", j ) ;
  96.          else
  97.             buffer [j] = bufptr ;
  98.       } ;
  99.       DisplaySize () ;                   /*  Display program size          */
  100.    } ;
  101.    DumpHeap ( "After all Reallocations - Top Down" ) ;   
  102.  
  103.       /*  Next, reallocate the blocks starting with the lowest
  104.           block and working upwards.                               */
  105.  
  106.    printf ( "Starting Reallocation - Bottom Up\n" ) ;
  107.    for ( j = 0 ; j < max ; j++ ) {
  108.       mem  = randnum ( 1, 1024  ) ;      /*  Generate reallocation size    */
  109.       if ( buffer [j] != NULL ) {
  110.          bufptr = (char *) realloc ( buffer [j], mem ) ;
  111.          if ( bufptr == NULL )
  112.             printf ( "Unable to realloc buffer %d\n", j ) ;
  113.          else
  114.             buffer [j] = bufptr ;
  115.       } ;
  116.       DisplaySize () ;                   /*  Display program size          */
  117.    } ;
  118.    DumpHeap ( "After all Reallocations - Bottom Up" ) ;   
  119.    
  120.       /*  Lastly, free all allocated blocks starting with the
  121.           last block in buffer array (which may now not be the
  122.           last block in memory).                                */
  123.  
  124.    printf ( "Starting Free\n" ) ;
  125.    while ( --max >= 0 ) {
  126.       if ( buffer [max] != NULL )
  127.          free ( buffer [max] ) ;
  128. #ifndef MSC
  129.       ReduceAllocation ( 1 ) ;           /*  Reduce allocation to minimum  */
  130. #endif
  131.       DisplaySize () ;                   /*  Display program size          */
  132.    } ;
  133.    DumpHeap ( "Finish" ) ; 
  134. }
  135.  
  136. randnum ( start, end ) 
  137.  
  138.    int  start ;
  139.    int  end   ;
  140.  
  141. /*
  142.    +------------------------------------------------------+
  143.    |                                                      |
  144.    |  This procedure generates a random integer number    |
  145.    |  in the range:                                       |
  146.    |                                                      |
  147.    |          start <= randnum <= end                     |
  148.    |                                                      |
  149.    +------------------------------------------------------+
  150. */
  151.  
  152. {
  153.    int  number ;
  154.    int  range  ;
  155.    long junk   ;
  156.  
  157.    range  = end - start + 1 ;
  158.    number = rand () ;
  159.    junk   = number  ;
  160.    junk   = ((long) range * junk) / 077777 + (long) start ;
  161.    number = (int) junk ;
  162.    if ( number < start ) number = start ;
  163.    if ( number > end   ) number = end ;
  164.    return ( number ) ;
  165. }
  166.